<?php
/**
 * 71CMS [ 创先云智慧党建系统 ]
 * =========================================================
 * Copyright (c) 2018-2023 南宁小橙科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.71cms.net
 * 这不是一个自由软件！未经许可不能去掉71CMS相关版权。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */
namespace app\common\model;

use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use think\Model;

class Sms extends Model
{
    private $config = [];

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $data = getConfig();

        $config = [
            'sms_template_id'=>$data['sms_template_id']??'',
            // HTTP 请求的超时时间（秒）
            'timeout' => 5.0,

            // 默认发送配置
            'default' => [
                // 网关调用策略，默认：顺序调用
                'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

                // 默认可用的发送网关
                'gateways' => [
//                    'yunpian',
                    'aliyun',
                ],
            ],
            // 可用的网关配置
            'gateways' => [
                'errorlog' => [
                    'file' => '/tmp/easy-sms.log',
                ],
//                'yunpian' => [
//                    'api_key' => '51a83786664d18735385eca8d330151a',
//                ],
                'aliyun' => [
                    'access_key_id' => $data['sms_access_key_id']??'',
                    'access_key_secret' => $data['sms_access_key_secret']??'',
                    'sign_name' => $data['sms_sign_name']??'',
                ],
                'juhe' => [
                    'gateway' => 'juhe',
                    'status' => 'failure',
                    'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException::class
                ],

                //...
            ],
        ];
        $this->config = $config;
    }


    /**
     * @param string $mobile
     * @param array $content['title|type_name','content','url']
     * @param string $templateID  申请的消息模板ID号，默认不填读配置
     * @return array
     */
    public function send(string $mobile='', array $content=[], string $templateID=''){
        if(empty($mobile)){
            return ['Code'=>0,'Message'=>'请填写手机号'];
        }
        if(empty($templateID)){
            if(empty($this->config['sms_template_id'])||!isset($this->config['sms_template_id'])){
                return ['Code'=>0,'Message'=>'请填写模板id'];
            }else{
                $templateID = $this->config['sms_template_id'];
            }
        }
        if(empty($content)){
            return ['Code'=>0,'Message'=>'请填写正确的参数'];
        }
        $easySms = new EasySms($this->config);

        try{
            $res = $easySms->send($mobile, [
//                'content'  => '您的验证码为: 6379',
                'template' => $templateID,
                'data' => $content,
            ]);
        }catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $e){
            $message = $e->getException('aliyun')->getMessage();
            $code = $e->getException('aliyun')->getCode();
            return ['Code'=>$code,'Message'=>$message];
        }
        return $res['aliyun']['result'];
    }
}
